/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.ui.controls
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import ua.kiev.gcore.logger.Logger;
	
	public class Button extends Sprite implements IButton
	{
		protected static const BUTTON_UP_STATE:int = 1;
		protected static const BUTTON_DOWN_STATE:int = 2;
		protected static const BUTTON_OVER_STATE:int = 3;
		protected static const BUTTON_DISABLE_STATE:int = 4;
		
		public var BUTTON_UP_FILTER:Array;
		public var BUTTON_DOWN_FILTER:Array;
		public var BUTTON_OVER_FILTER:Array;
		public var BUTTON_DISABLE_FILTER:Array;
		
		
		
		private var _UID:String;
		private var _State:int;
		private var _Enabled:Boolean;
		private var _Size:Point;
		private var mouseClickFunction:Function;
		
		protected var backGround:DisplayObject;
		protected var textField:TextField;
		
		private var buttonIsDown:Boolean;
		
		public function Button(UID:String, backGround:DisplayObject, textField:TextField, textValue:String, mouseClickFunction:Function)
		{
			super();
			
			buttonIsDown = false;
			
			BUTTON_UP_FILTER = [];
			BUTTON_DOWN_FILTER = [];
			BUTTON_OVER_FILTER = [];
			BUTTON_DISABLE_FILTER = [];
			
			this._UID = UID;			
			this.textField = textField;
			this.mouseClickFunction = mouseClickFunction;
			
			if(backGround != null){
				this.backGround = backGround;
				this.addChild(backGround);
				
				Size = new Point(backGround.width, backGround.height);
			}
			
			if(textField != null){
				this.textField = textField;
				this.addChild(textField);
				
				if(textValue != null){
					textField.text = textValue;
				}
				
				if(!Size){
					Size = new Point(textField.width, textField.height);
				}
			}
			
			if(!Size){
				Size = new Point();
			}
			
			this.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			this.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);	
			this.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			
			Enabled = true;
		}
		
		public function set Listener(value:Function):void{
			mouseClickFunction = value;
		}
		public function get Listener():Function{
			return mouseClickFunction;
		}
		
		public function set Label(value:String):void{
			if((textField != null) && (value != null)){
				textField.text = value;
			}
		}
		
		public function get Label():String{
			if(textField != null){
				return textField.text;
			}
			
			return new String();
		}
		
		public function SetBackGroundPosition(position:Point):void{
			if(backGround != null){
				backGround.x = position.x;
				backGround.y = position.y;
			}			
		}
		
		public function SetBackGroundSize(size:Point):void{
			if(backGround != null){
				backGround.width = size.x;
				backGround.height = size.y;
			}			
		}
		
		public function destriy():void{
			if(hasEventListener(MouseEvent.MOUSE_UP)){
				removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			}
			if(hasEventListener(MouseEvent.MOUSE_DOWN)){
				removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			}
			if(hasEventListener(MouseEvent.MOUSE_OVER)){
				removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			}
			if(hasEventListener(MouseEvent.MOUSE_OUT)){
				removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			}
		}
		
		private function mouseUpHandler(event:MouseEvent):void{
			if(Enabled){				
				State = BUTTON_UP_STATE;
				filters = BUTTON_UP_FILTER;
				if(buttonIsDown){
					buttonIsDown = false;
					
					if(mouseClickFunction != null){
						try{
							mouseClickFunction(this);
						}catch(error:Error){
							Logger.ERROR(error.message);
						}
					}
				}
			}
		}
		
		private function mouseDownHandler(event:MouseEvent):void{
			if(Enabled){
				State = BUTTON_DOWN_STATE;
				filters = BUTTON_DOWN_FILTER;
				buttonIsDown = true;
			}
		}
		
		private function mouseOverHandler(event:MouseEvent):void{
			if(Enabled){				
				State = BUTTON_OVER_STATE;
				filters = BUTTON_OVER_FILTER;
			}
		}
		
		private function mouseOutHandler(event:MouseEvent):void{
			if(Enabled){				
				State = BUTTON_UP_STATE;
				filters = BUTTON_UP_FILTER;
				buttonIsDown = false;
			}
		}	
		
			
		
		
		
		public function get UID():String{
			return _UID;	
		}
		
		public function set Size(value:Point):void{
			_Size = value;
			if(backGround != null){
				backGround.width = Size.x;
				backGround.height = Size.y;
			}
			if(textField != null){
				textField.width = Size.x;
				textField.height = Size.y;
			}
		}
		
		public function get Size():Point{
			return _Size;	
		}
		
		public function set Enabled(value:Boolean):void{
			_Enabled = value;
			if(!Enabled){				
				State = BUTTON_DISABLE_STATE;
				filters = BUTTON_DISABLE_FILTER;
			}else{
				State = BUTTON_UP_STATE;
				filters = BUTTON_UP_FILTER;
			}
		}
		
		public function get Enabled():Boolean{
			return _Enabled;	
		}
		
		public function set State(value:int):void{
			_State = value;
		}
		
		public function get State():int{
			return _State;	
		}
	}
}